package com.eastfair.tag.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.tag.dto.TagResourceDTO;
import com.eastfair.tag.dto.TagResourcePageQuery;
import com.eastfair.tag.dto.TagResourceSaveDTO;
import com.eastfair.tag.dto.TagResourceUpdateDTO;
import com.eastfair.tag.entity.TagResource;
import com.eastfair.tag.service.TagResourceService;
import com.eastfair.tag.vo.TagResourceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 *
 * </p>
 *
 * @author zjn
 * @date 2022-03-11
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/tagResource")
@Api(value = "TagResource", tags = "")
public class TagResourceController extends SuperController<TagResourceService, Long, TagResource, TagResourcePageQuery, TagResourceSaveDTO, TagResourceUpdateDTO> {

    @Autowired
    private TagResourceService tagResourceService;

    @Override
    public void handlerResult(IPage<TagResource> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<TagResource> tagResourceList = list.stream().map((map) -> {
            TagResource tagResource = TagResource.builder().build();
            //TODO 请在这里完成转换
            return tagResource;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(tagResourceList));
    }


    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<TagResourcePageQuery> params) {
        TagResourcePageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 获取标签与资源的关系
     *
     * @param tagResourceDTOList 资源信息列表
     * @return 标签与资源的关系列表
     */
    @ApiOperation(value = "获取标签与资源的关系", notes = "获取标签与资源的关系")
    @PostMapping("/getTagResourceRelationList")
    public R<List<TagResourceVO>> getTagResourceRelationList(@RequestBody List<TagResourceDTO> tagResourceDTOList) {
        return tagResourceService.getTagResourceRelationList(tagResourceDTOList);
    }

    /**
     * 新增标签与资源的关系
     * 关联关系先删后插
     *
     * @param tagResourceDTO 标签与资源的关系
     * @return 是否成功
     */
    @ApiOperation(value = "新增标签与资源的关系", notes = "新增标签与资源的关系")
    @PostMapping("/saveTagResourceRelation")
    public R<Boolean> saveTagResourceRelation(@RequestBody TagResourceDTO tagResourceDTO) {
        return tagResourceService.saveTagResourceRelation(tagResourceDTO);
    }

    /**
     * 删除标签与资源的关系
     *
     * @param tagResourceDTO 资源信息
     * @return 是否成功
     */
    @ApiOperation(value = "删除标签与资源的关系", notes = "删除标签与资源的关系")
    @PostMapping("/deleteTagResourceRelation")
    public R<Boolean> deleteTagResourceRelation(@RequestBody TagResourceDTO tagResourceDTO) {
        return tagResourceService.deleteTagResourceRelation(tagResourceDTO);
    }

}
